package cn.honqun.tinycms.config;

import cn.dev33.satoken.stp.StpInterface;
import cn.honqun.tinycms.domain.entity.Role;
import cn.honqun.tinycms.domain.entity.User;
import cn.honqun.tinycms.service.UserService;
import cn.hutool.core.util.NumberUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Component
@RequiredArgsConstructor
@Slf4j
public class StpInterfaceImpl implements StpInterface {
    private final UserService userService;

    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        log.debug("用户鉴权 {} {}", loginId, loginType);

        if (loginId == null) {
            return Collections.emptyList();
        }
        if (!NumberUtil.isNumber(loginId.toString())) {
            return Collections.emptyList();
        }

        User user = new User(NumberUtil.parseLong(loginId.toString()));
        List<Role> roles = userService.selectRoles(user);
        return roles.stream().map(Role::getRole).collect(Collectors.toList());
    }

    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        return Collections.emptyList();
    }
}
